;-------------------------------------------------------------------------------
format ELF64 executable 3
entry start

SDL_INIT_TIMER = 0x00000001
SDL_INIT_VIDEO = 0x00000020
SDL_GL_RED_SIZE = 0
SDL_GL_GREEN_SIZE = 1
SDL_GL_BLUE_SIZE = 2
SDL_GL_DOUBLEBUFFER = 5
SDL_WINDOW_OPENGL = 0x00000002
SDL_WINDOW_SHOWN = 0x00000004
SDL_WINDOW_BORDERLESS = 0x00000010
SDL_KEYDOWN = 0x300

GL_VERTEX_SHADER = 0x8B31
GL_FRAGMENT_SHADER = 0x8B30

WIN_X = 10
WIN_Y = 10
WIN_W = 1024
WIN_H = 768

DEBUG = 0

macro emit [param] {param}
;-------------------------------------------------------------------------------
include 'import64.inc'
interpreter '/lib64/ld-linux-x86-64.so.2'
needed 'libdl.so.2'
import dlopen,dlsym
;-------------------------------------------------------------------------------
segment readable executable

start:
    mov rbp,rsp
    sub rsp,2048
    ; define local variables
    emit\
        <glCreateShader equ rbp-8>,<glShaderSource equ rbp-16>,\
        <glCompileShader equ rbp-24>,<glCreateProgram equ rbp-32>,\
        <glLinkProgram equ rbp-40>,<glUseProgram equ rbp-48>,\
        <glAttachShader equ rbp-56>,<glGetUniformLocation equ rbp-64>,\
        <glUniform1f equ rbp-72>,<glUniform2i equ rbp-80>,<glRecti equ rbp-88>,\
        <SDL_PollEvent equ rbp-144>,<SDL_GetTicks equ rbp-152>,\
        <SDL_GL_SetAttribute equ rbp-160>,<SDL_GL_SwapWindow equ rbp-168>,<SDL_GL_GetProcAddress equ rbp-176>,\
        <time_loc equ rbp-384>,<sh equ rbp-392>,<prog equ rbp-400>,<libsdl equ rbp-408>,<win equ rbp-416>,<event equ rbp-768>,\
        <printf equ rbp-800>,<libc equ rbp-808>,\
        <log equ rbp-2048>
if DEBUG = 1
    ; load libc
    emit <mov rdi,g_str_libc>,<xor esi,esi>,<inc esi>,<call [dlopen]>,<mov [libc],rax>
    emit <mov rdi,[libc]>,<mov rsi,g_str_printf>,<call [dlsym]>,<mov [printf],rax>
end if
    ; load libSDL-1.3.so.0
    emit <mov rdi,g_str_libsdl>,<xor esi,esi>,<inc esi>,<call [dlopen]>,<mov [libsdl],rax>
    ; SDL_Init
    emit <mov rdi,[libsdl]>,<mov rsi,g_str_SDL_Init>,<call [dlsym]>
    emit <mov edi,SDL_INIT_TIMER+SDL_INIT_VIDEO>,<call rax>
    ; set window attributes
    emit <mov rdi,[libsdl]>,<mov rsi,g_str_SDL_GL_SetAttribute>,<call [dlsym]>,<mov [SDL_GL_SetAttribute],rax>
    emit <mov edi,SDL_GL_DOUBLEBUFFER>,<mov esi,1>,<call qword [SDL_GL_SetAttribute]>
    emit <mov edi,SDL_GL_RED_SIZE>,<mov esi,8>,<call qword [SDL_GL_SetAttribute]>
    emit <mov edi,SDL_GL_GREEN_SIZE>,<mov esi,8>,<call qword [SDL_GL_SetAttribute]>
    emit <mov edi,SDL_GL_BLUE_SIZE>,<mov esi,8>,<call qword [SDL_GL_SetAttribute]>
    ; create window
    emit <mov rdi,[libsdl]>,<mov rsi,g_str_SDL_CreateWindow>,<call [dlsym]>
    emit <mov rdi,g_str_SDL_CreateWindow>,<mov esi,WIN_X>,<mov edx,WIN_Y>,<mov ecx,WIN_W>,<mov r8d,WIN_H>,\
        <mov r9d,SDL_WINDOW_SHOWN+SDL_WINDOW_OPENGL+SDL_WINDOW_BORDERLESS>,<call rax>,<mov [win],rax>
    ; create context
    emit <mov rdi,[libsdl]>,<mov rsi,g_str_SDL_GL_CreateContext>,<call [dlsym]>
    emit <mov rdi,[win]>,<call rax>
    ; load needed SDL functions
    emit <mov rdi,[libsdl]>,<mov rsi,g_str_SDL_PollEvent>,<call [dlsym]>,<mov [SDL_PollEvent],rax>
    emit <mov rdi,[libsdl]>,<mov rsi,g_str_SDL_GL_SwapWindow>,<call [dlsym]>,<mov [SDL_GL_SwapWindow],rax>
    emit <mov rdi,[libsdl]>,<mov rsi,g_str_SDL_GL_GetProcAddress>,<call [dlsym]>,<mov [SDL_GL_GetProcAddress],rax>
    emit <mov rdi,[libsdl]>,<mov rsi,g_str_SDL_GetTicks>,<call [dlsym]>,<mov [SDL_GetTicks],rax>
    ; load needed GL commands
    emit <mov rdi,g_str_glCreateShader>,<call qword [SDL_GL_GetProcAddress]>,<mov [glCreateShader],rax>
    emit <mov rdi,g_str_glShaderSource>,<call qword [SDL_GL_GetProcAddress]>,<mov [glShaderSource],rax>
    emit <mov rdi,g_str_glCompileShader>,<call qword [SDL_GL_GetProcAddress]>,<mov [glCompileShader],rax>
    emit <mov rdi,g_str_glCreateProgram>,<call qword [SDL_GL_GetProcAddress]>,<mov [glCreateProgram],rax>
    emit <mov rdi,g_str_glLinkProgram>,<call qword [SDL_GL_GetProcAddress]>,<mov [glLinkProgram],rax>
    emit <mov rdi,g_str_glUseProgram>,<call qword [SDL_GL_GetProcAddress]>,<mov [glUseProgram],rax>
    emit <mov rdi,g_str_glAttachShader>,<call qword [SDL_GL_GetProcAddress]>,<mov [glAttachShader],rax>
    emit <mov rdi,g_str_glGetUniformLocation>,<call qword [SDL_GL_GetProcAddress]>,<mov [glGetUniformLocation],rax>
    emit <mov rdi,g_str_glUniform1f>,<call qword [SDL_GL_GetProcAddress]>,<mov [glUniform1f],rax>
    emit <mov rdi,g_str_glUniform2i>,<call qword [SDL_GL_GetProcAddress]>,<mov [glUniform2i],rax>
    emit <mov rdi,g_str_glRecti>,<call qword [SDL_GL_GetProcAddress]>,<mov [glRecti],rax>
    ; create GLSL program
    emit <call qword [glCreateProgram]>,<mov [prog],eax>
    ; create and attach vertex shader
    emit <mov edi,GL_VERTEX_SHADER>,<call qword [glCreateShader]>,<mov [sh],eax>
    emit <mov edi,[prog]>,<mov esi,[sh]>,<call qword [glAttachShader]>
    emit <mov edi,[sh]>,<mov esi,1>,<mov rdx,g_src_vs_ptr>,<xor ecx,ecx>,<call qword [glShaderSource]>
    emit <mov edi,[sh]>,<call qword [glCompileShader]>
    ; create and attach fragment shader
    emit <mov edi,GL_FRAGMENT_SHADER>,<call qword [glCreateShader]>,<mov [sh],eax>
    emit <mov edi,[prog]>,<mov esi,[sh]>,<call qword [glAttachShader]>
    emit <mov edi,[sh]>,<mov esi,1>,<mov rdx,g_src_fs_ptr>,<xor ecx,ecx>,<call qword [glShaderSource]>
    emit <mov edi,[sh]>,<call qword [glCompileShader]>
    ; link and use program
    emit <mov edi,[prog]>,<call qword [glLinkProgram]>
    emit <mov edi,[prog]>,<call qword [glUseProgram]>
if DEBUG = 1
    ; print GLSL info log
    emit <mov rdi,g_str_glGetProgramInfoLog>,<call qword [SDL_GL_GetProcAddress]>
    emit <mov edi,[prog]>,<mov esi,1024>,<xor edx,edx>,<lea rcx,[log]>,<call rax>
    emit <mov rdi,g_str_log>,<lea rsi,[log]>,<call qword [printf]>
end if
    ; get <time> uniform location
    emit <mov edi,[prog]>,<mov rsi,g_str_time>,<call qword [glGetUniformLocation]>,<mov [time_loc],eax>
    ; set <res> uniform
    emit <mov edi,[prog]>,<mov rsi,g_str_res>,<call qword [glGetUniformLocation]>
    emit <mov edi,eax>,<mov esi,WIN_W>,<mov edx,WIN_H>,<call qword [glUniform2i]>
    ; enter main loop
      .main_loop:
    ; process window events
    emit <lea rdi,[event]>,<call qword [SDL_PollEvent]>
    cmp dword [event+0],SDL_KEYDOWN
    je .main_loop_end
    ; get time
    emit <call qword [SDL_GetTicks]>
    ; set <time> uniform
    emit <cvtsi2ss xmm0,eax>,<divss xmm0,[g_ticks_per_sec]>,<mov edi,[time_loc]>,<call qword [glUniform1f]>
    ; draw
    emit <mov edi,-1>,<mov esi,-1>,<mov edx,1>,<mov ecx,1>,<call qword [glRecti]>
    ; swap
    emit <mov rdi,[win]>,<call qword [SDL_GL_SwapWindow]>
    jmp .main_loop
      .main_loop_end:
    ; terminate process
    emit <mov rdi,[libsdl]>,<mov rsi,g_str_SDL_Quit>,<call [dlsym]>,<call rax>
    emit <mov eax,60>,<xor edi,edi>,syscall
;-------------------------------------------------------------------------------
segment readable writeable

g_ticks_per_sec dd 1000.0

g_src_vs:
db  '#version 400 compatibility',10
db  'void main(){'
db      'gl_Position=gl_Vertex;'
db  '}',0

g_src_fs:
db  '#version 400 compatibility',10
db  'uniform float time;'
db  'uniform ivec2 res;'
db  'vec4 mod289(vec4 x){'
db      'return x-floor(x*(1./289.))*289.;'
db  '}'
db  'vec4 permute(vec4 x){'
db      'return mod289(((x*34.)+1.)*x);'
db  '}'
db  'vec4 taylorInvSqrt(vec4 r){'
db      'return 1.79284291400159-0.85373472095314*r;'
db  '}'
db  'vec2 fade(vec2 t){'
db      'return t*t*t*(t*(t*6.-15.)+10.);'
db  '}'
db  'float pnoise(vec2 P,vec2 rep){'
db      'vec4 Pi=floor(P.xyxy)+vec4(0.,0.,1.,1.);'
db      'vec4 Pf=fract(P.xyxy)-vec4(0.,0.,1.,1.);'
db      'Pi=mod(Pi,rep.xyxy);'
db      'Pi=mod289(Pi);'
db      'vec4 ix=Pi.xzxz;'
db      'vec4 iy=Pi.yyww;'
db      'vec4 fx=Pf.xzxz;'
db      'vec4 fy=Pf.yyww;'
db      'vec4 i=permute(permute(ix)+iy);'
db      'vec4 gx=fract(i*(1./41.))*2.-1.;'
db      'vec4 gy=abs(gx)-.5;'
db      'vec4 tx=floor(gx+.5);'
db      'gx=gx-tx;'
db      'vec2 g00=vec2(gx.x,gy.x);'
db      'vec2 g10=vec2(gx.y,gy.y);'
db      'vec2 g01=vec2(gx.z,gy.z);'
db      'vec2 g11=vec2(gx.w,gy.w);'
db      'vec4 norm=taylorInvSqrt(vec4(dot(g00,g00),dot(g01,g01),dot(g10,g10),dot(g11,g11)));'
db      'g00*=norm.x;'
db      'g01*=norm.y;'
db      'g10*=norm.z;'
db      'g11*=norm.w;'
db      'float n00=dot(g00,vec2(fx.x,fy.x));'
db      'float n10=dot(g10,vec2(fx.y,fy.y));'
db      'float n01=dot(g01,vec2(fx.z,fy.z));'
db      'float n11=dot(g11,vec2(fx.w,fy.w));'
db      'vec2 fade_xy=fade(Pf.xy);'
db      'vec2 n_x=mix(vec2(n00,n01),vec2(n10,n11),fade_xy.x);'
db      'float n_xy=mix(n_x.x,n_x.y,fade_xy.y);'
db      'return 2.3*n_xy;'
db  '}'
db  'vec3 eye(float x,float y){'
db      'y-=.05;'
db      'x-=.1;'
db      'float rx=2.*(x-.5)*1.333;'
db      'float ry=2.*(y-.5);'
db      'float a=atan(ry,rx);'
db      'float e=rx*rx+ry*ry;'
db      'float r=sqrt(e);'
db      'vec3 fc=vec3(1.,1.,1.);'
db      'float ven=pnoise(vec2(28.*x,28.*y),vec2(256.,256.));'
db      'ven=smoothstep(-.2,.0,ven)-smoothstep(.0,.2,ven);'
db      'ven+=x+x*x*x*x*x*x*x*7.;'
db      'fc+=.04-vec3(.0,.07,.07)*ven;'
db      'vec3 dc=vec3(.3,.7,.4+e);'
db      'float no=.8+.2*pnoise(vec2(4.*r,32.*a/3.141592654),vec2(32.,32.));'
db      'dc*=no;'
db      'dc*=smoothstep(.025,.035,e);'
db      'vec3 col=mix(dc,fc,smoothstep(.35,.36,e));'
db      'col*=1.-.35*(smoothstep(.31,.35,e)-smoothstep(.35,.39,e));'
db      'float r1=sqrt(r*r*r);'
db      'float re=pnoise(vec2(.075*time+4.*r1*cos(a+.05*time),4.*r1*sin(a+.05*time)),vec2(256.,256.));'
db      're=.8*smoothstep(.1,.5,re);'
db      'col+=re*(1.-vec3(col));'
db      'return col;'
db  '}'
db  'void main(){'
db      'vec2 p=gl_FragCoord.xy/res;'
db      'float rx=2.*(p.x-.5)*1.333;'
db      'float ry=2.*(p.y-.5);'
db      'float h=3.*sqrt(p.x*p.x*p.x)*(1.0-p.x);'
db      'float e=abs(ry)-h;'
db      'float f=smoothstep(.0,.025,e);'
db      'gl_FragColor=vec4(mix(eye(p.x,p.y),vec3(0.),f),1.);'
db '}',0

align 8
g_src_vs_ptr dq g_src_vs
g_src_fs_ptr dq g_src_fs

g_str_SDL_Init db 'SDL_Init',0
g_str_SDL_Quit db 'SDL_Quit',0
g_str_SDL_CreateWindow db 'SDL_CreateWindow',0
g_str_SDL_PollEvent db 'SDL_PollEvent',0
g_str_SDL_GetTicks db 'SDL_GetTicks',0
g_str_SDL_GL_SetAttribute db 'SDL_GL_SetAttribute',0
g_str_SDL_GL_CreateContext db 'SDL_GL_CreateContext',0
g_str_SDL_GL_SwapWindow db 'SDL_GL_SwapWindow',0
g_str_SDL_GL_GetProcAddress db 'SDL_GL_GetProcAddress',0
g_str_glCreateShader db 'glCreateShader',0
g_str_glShaderSource db 'glShaderSource',0
g_str_glCompileShader db 'glCompileShader',0
g_str_glCreateProgram db 'glCreateProgram',0
g_str_glLinkProgram db 'glLinkProgram',0
g_str_glUseProgram db 'glUseProgram',0
g_str_glAttachShader db 'glAttachShader',0
g_str_glGetUniformLocation db 'glGetUniformLocation',0
g_str_glUniform1f db 'glUniform1f',0
g_str_glUniform2i db 'glUniform2i',0
g_str_glRecti db 'glRecti',0
g_str_libsdl db 'libSDL-1.3.so.0',0
g_str_time db 'time',0
g_str_res db 'res',0
if DEBUG = 1
g_str_libc db 'libc.so.6',0
g_str_printf db 'printf',0
g_str_glGetProgramInfoLog db 'glGetProgramInfoLog',0
g_str_log db 'GLSL info log:',10,'%s',10,0
end if
;-------------------------------------------------------------------------------
